package magicloan.auctionpocket.ui.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.adapter.ReFirstFragmentAdapter;
import magicloan.auctionpocket.bean.ProduceBean;
import magicloan.auctionpocket.decoration.SpacesItemDecoration;
import magicloan.auctionpocket.ui.base.BaseFragment;

import static magicloan.auctionpocket.ui.base.MyReBaseAdapter.TYPE_HEADER;

/**
 * Created by hechao on 2018/1/25.
 */
public class FirstFragment extends BaseFragment {

    //    private FragmentFirstReAdapter mAdapter;
    private ReFirstFragmentAdapter mRecylerAdapter;
    private RecyclerView mRecyclerView;
    private ArrayList<ProduceBean> mReDateList;
    private ArrayList mHeaderReDateList;

    @Override
    protected int setFgContentView() {
        return R.layout.fg_first;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void beforeInitView() {
//        verticalTvHelper = new VerticalTextViewHelper(mActivity);
        mReDateList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            ProduceBean instance = new ProduceBean();
            instance.hasTime = 10 + i * 2;  // TODO 配上列表假数据
            mReDateList.add(instance);
        }
        mHeaderReDateList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ProduceBean instance = new ProduceBean();
            mHeaderReDateList.add(instance); // TODO 配上Header假数据
        }
        mRecylerAdapter = new ReFirstFragmentAdapter(mActivity, mReDateList, mHeaderReDateList);
    }

    @Override
    protected void afterInitView() {

        mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.recylerv);

        mRecyclerView.setLayoutManager(createManager());

        mRecyclerView.addItemDecoration(new SpacesItemDecoration(10));

        mRecyclerView.setAdapter(mRecylerAdapter);
        mRecylerAdapter.mRecyclerView = mRecyclerView;

        mRecylerAdapter.notifyDataSetChanged();

//        //GV
//        GridView gv_identify_mainfg = (GridView) mContentView.findViewById(R.id.gv_identify_mainfg);
//        gv_identify_mainfg.setAdapter(mGvAdapter);
//        mGvAdapter.setDatas(mReDateList);
//        mGvAdapter.notifyDataSetChanged();

        mViewEmpty = mContentView.findViewById(R.id.view_empty);
        mViewEmpty.setVisibility(View.INVISIBLE);

    }

    private GridLayoutManager createManager() {
        final GridLayoutManager nManager = new GridLayoutManager(mActivity, 2);
        nManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mRecylerAdapter.getItemViewType(position) == TYPE_HEADER
                        ? nManager.getSpanCount() : 1;
            }
        });
        return nManager;
    }


    private class MyRequestLoadMoreListener implements BaseQuickAdapter.RequestLoadMoreListener {

        @Override
        public void onLoadMoreRequested() {
//            if (!isLoadMore) {
//                isLoadMore = true;
//                currentPage = ++currentPage;
//                mService.getRefreshLoanData(NetConstance.GetLoanData, currentPage, 6);
//            }
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void afterConnectSuccess(String flag, JSONObject request) throws JSONException {

    }

    @Override
    public void afterConnectFail(String flag, JSONObject result, String cache) throws JSONException {

    }

}
